package com.keehin.migrate;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.keehin.Database;

public class MigrateContact {
	protected static Database srcDB;
	protected static Database descDB;
	private static ResultSet rsAdrBody;
	private static ResultSet rsAddrDept;
	private static ResultSet rsAddrGroup;

	public static void main(String[] args) {
		if (args[3].equals("#"))
			args[3] = "";
		try {
			srcDB = new Database(new com.mysql.jdbc.Driver(), "jdbc:mysql://"
					+ args[0] + "?characterEncoding=tis620", args[2], args[3]);
			if (srcDB != null)
				System.out.println(srcDB + " OK");
			descDB = new Database(new com.mysql.jdbc.Driver(), "jdbc:mysql://"
					+ args[1] + "?characterEncoding=tis620", args[2], args[3]);
			if (descDB != null)
				System.out.println(descDB + " OK");
		} catch (SQLException e) {
			e.printStackTrace();
		}

		srcDB.createSQL("lookupDept", "select * from addrdept");

		Object[] adrIdent = new Object[1];
	
		//rsAdrBody = srcDB.executeQuery("select * from adrbody");
		int i = 1;
		//descDB.executeUpdate("delete from contact");
		// descDB.executeUpdate("delete from contactgroup");
		// descDB.executeUpdate("insert into windmill.contactgroup(comCode,grpId, grpName)   select '"
		// + args[4] + "',AdrGrpRef, GroupName from migrate.adrgroup");

		descDB.createSQL(
				"insert",
				"insert into contact (comCode,contCode,prefix,name,surname,taxid,organiz,branch,address,"
						+ "tambon,amphur,province,postCode,addLine,phone,fax,mobile,email,note,label,creUser)"
						+ " values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
		Object[] params = new Object[21];
		params[0] = args[4];
		try {
			/*
			while (rsAdrBody.next()) {
				for (int x = 1; x < params.length; x++)
					params[x] = null;
				params[1] = rsAdrBody.getString("Ident");
				params[2] = rsAdrBody.getString("TitleQualifier");
				params[3] = rsAdrBody.getString("Title");
				params[4] = rsAdrBody.getString("Name");
				params[5] = rsAdrBody.getString("newIdent");

				if (params[3] != null) {
					if (params[3].toString().length() > 70) {
						params[3] = params[3].toString().substring(0, 69);
					}
				}
				if (params[6] != null) {
					if (params[6].toString().length() > 70) {
						params[6] = params[6].toString().substring(0, 69);
					}
				}
				// params[7] = rsAdrBody.getString("oldIdent");
				params[8] = rsAdrBody.getString("Road");
				params[9] = rsAdrBody.getString("Tambon");
				params[10] = rsAdrBody.getString("Amphur");
				params[11] = rsAdrBody.getString("Town");
				params[12] = rsAdrBody.getString("PostCode");
				params[13] = rsAdrBody.getString("AddLine");
				params[14] = rsAdrBody.getString("Tel");
				params[15] = rsAdrBody.getString("Fax");
				params[16] = rsAdrBody.getString("MobPhone");
				if (rsAdrBody.getString("EMAil") != null) {
					String[] words = rsAdrBody.getString("EMAil").split(" ");
					for (int j = 0; j < words.length; j++) {
						if (words[j].indexOf("@") > -1)
							params[17] = words[j];
						else {
							if (params[18] == null)
								params[18] = words[j];
							else
								params[18] = params[18] + " " + words[j];
						}
					}

				}
				params[19] = rsAdrBody.getString("LabelAddress");
				params[20] = "admin";
				descDB.execSqlMap("insert", params);
				printSymbol(i++);
			}
			rsAdrBody.close();
			rsAddrDept.close();
			*/
			rsAddrGroup = srcDB.executeQuery("select  Ident, adrGrpRef "
					+ "from adrgrps, adrbody "
					+ "where adrgrps.adrRefNr = adrbody.AdrRefNum");
			descDB.executeUpdate("delete from contactgrping");
			descDB.createSQL("insGrp",
					"insert into windmill.contactgrping(comCode, contCode, grpId) values (?,?,?)");
			params = new Object[3];
			params[0] = args[4];
			while (rsAddrGroup.next()) {
				params[1] = rsAddrGroup.getString(1);
				params[2] = rsAddrGroup.getInt(2);
				descDB.execSqlMap("insGrp", params);
			}
	
			rsAddrDept = srcDB.executeQuery("select * from addrdept");
			descDB.executeUpdate("delete from contdept");
			descDB.createSQL("insdept",
					"insert into windmill.contdept values (?,?,?,?,?,?,?,?,?,?)");
			params = new Object[10];
			params[0] = args[4];
			i = 0;
			while (rsAddrDept.next()) {
				i = i + 1;
				printSymbol(i);
				params[1] = rsAddrDept.getString(1);			
				params[2] = rsAddrDept.getString(2);
				if (params[2] == null) params[2] = "ทั่วไป";

				params[3] = rsAddrDept.getString(4);
				params[4] = rsAddrDept.getString(3);
				params[5] = rsAddrDept.getString(5);
				params[6] = rsAddrDept.getString(6);
				params[7] = rsAddrDept.getString(7);
				params[8] = rsAddrDept.getString(8);
				params[9] = rsAddrDept.getString(9);
				descDB.execSqlMap("insdept", params);
			}
			/**
			 * adrIdent[0] = params[1]; rsAddrDept =
			 * srcDB.querySqlMap("lookupDept", adrIdent); StringBuilder sb = new
			 * StringBuilder( params[17] != null ? params[17].toString() + '\n'
			 * : ""); while (rsAddrDept.next()) {
			 * sb.append(rsAddrDept.getString(2) == null ? "" : rsAddrDept
			 * .getString(2) + ",");
			 * sb.append(rsAddrDept.getString("NameQualifier") == null ? "" :
			 * (" " + rsAddrDept.getString("NameQualifier")));
			 * 
			 * sb.append(rsAddrDept.getString("FirstName") == null ? "" :
			 * rsAddrDept.getString("FirstName"));
			 * 
			 * sb.append(rsAddrDept.getString("Name") == null ? "" : " " +
			 * rsAddrDept.getString("Name"));
			 * 
			 * sb.append(rsAddrDept.getString("Tel") == null ? "" : (" " +
			 * rsAddrDept.getString("Tel")));
			 * sb.append(rsAddrDept.getString("Fax") == null ? "" : (" " +
			 * rsAddrDept.getString("Fax")));
			 * sb.append(rsAddrDept.getString("MobPhone") == null ? "" : (" " +
			 * rsAddrDept.getString("MobPhone"))); sb.append('\n'); } params[18]
			 * = sb.toString(); if (params[18] != null) { if
			 * (params[18].toString().length() > 256) { //
			 * System.out.println(params[12].toString().length() // +
			 * " *******************"); params[18] =
			 * params[18].toString().substring(0, 255); } }
			 */
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void printSymbol(int i) {
		if (i % 500 == 0) {
			System.out.print("#");
			if (i % 10000 == 0)
				System.out.printf("%,10d\n", i);
			else if (i % 50000 == 0)
				System.out.println();
		}
	}
}
